package com.hb0730.boot.admin.modules.sci.controller;

import com.hb0730.boot.admin.base.R;
import com.hb0730.boot.admin.modules.sci.model.entity.SciAccount;
import com.hb0730.boot.admin.modules.sci.model.entity.SciPaperConference;
import com.hb0730.boot.admin.modules.sci.model.query.SciAccountTree;
import com.hb0730.boot.admin.modules.sci.service.SciAccountService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/sci/account")
@RequiredArgsConstructor
public class SciAccountController {
    private final SciAccountService sciAccountService;
    @GetMapping("/tree")
    @Operation(summary = "具体树")
    public R<List<SciAccountTree>> treesAccount(HttpServletRequest request, @RequestParam String type) {
        Optional<List<SciAccount>> accountOpt = sciAccountService.listAccount(type);
        Optional<List<SciAccountTree>> treeOpt = accountOpt
                .flatMap(e -> Optional.ofNullable(sciAccountService.buildTree(e)));
        List<SciAccountTree> tree = treeOpt.orElse(Collections.emptyList());
        return R.OK(tree);
    }
    @GetMapping("/treeAll")
    @Operation(summary = "添加树")
    public R<List<SciAccountTree>> treesAccountAll() {
        Optional<List<SciAccount>> accountOpt = sciAccountService.listAccount(null);
        Optional<List<SciAccountTree>> treeOpt = accountOpt
                .flatMap(e -> Optional.ofNullable(sciAccountService.buildTree(e)));
        List<SciAccountTree> tree = treeOpt.orElse(Collections.emptyList());
        return R.OK(tree);
    }

    @PostMapping("/save")
    @Operation(summary = "保存菜单与权限")
    @PreAuthorize("@permission.hashPermission('menu:save')")
    public R<String> save(@RequestBody SciAccount sciAccount) {
        return sciAccountService.save(sciAccount) ? R.OK("添加成功") : R.NG("保存失败");
    }

    @PutMapping("/update")
    @Operation(summary = "修改")
    public R<String> update(HttpServletRequest request,
                                        @Validated @RequestBody SciAccount sciAccount) {
        return sciAccountService.updateById(sciAccount)? R.OK("更新成功") : R.NG("更新失败");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    public R<String> delete(HttpServletRequest request, @Validated @RequestBody  String id) {

        return sciAccountService.removeById(id)? R.OK("删除成功") : R.NG("删除失败");
    }

    @GetMapping("/time")
    @Operation(summary = "添加树")
    public R<Map<String, Object>> getTime() {
        return R.OK(sciAccountService.getTime());
    }
    @PutMapping("/updateTime")
    @Operation(summary = "修改")
    public R<String> updateTime(HttpServletRequest request,
                                        @Validated @RequestBody Map<String, Object> timeAccount) {
        return sciAccountService.updateTime(timeAccount) > 0
                ? R.OK("更新成功")
                : R.NG("更新失败");
    }
    @GetMapping("/checkTime")
    @Operation(summary = "添加树")
    public R<String> checkTime() {
        return sciAccountService.checkTime()? R.OK("时间正确") : R.NG("不在审核时间范围内");
    }
}
